package com.itbaizhan;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/*获取容器的附加信息

servletContext.getServerInfo()
返回Servlet容器的名称和版本号

servletContext.getMajorVersion()
返回Servlet容器所支持Servlet的主版本号。

servletContext.getMinorVersion()
返回Servlet容器所支持Servlet的副版本号。
*/
public class GetBaseInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取容器对象
        ServletContext servletContext = this.getServletContext();
        //获取容器支持servlet的主版本号
        String  serverInfo =servletContext.getServerInfo();
        //获取容器支持servlet的主版本号
        int majorVersion =servletContext.getMajorVersion();
        //获取容器servlet副版本号
        int minorVersion =servletContext.getMinorVersion();

        //因为返回来的有中文 所以要设置响应编码
        resp.setContentType("text/plain;charset=utf-8");
        //在浏览器中输出
        PrintWriter pw = resp.getWriter();
        pw.println("容器基本信息: "+serverInfo);
        pw.println("版本号："+majorVersion+"."+minorVersion);

        //servletContext的全局性  在其它的servlet对象也可以获取容器
        String value = (String)servletContext.getAttribute("key1");
        String value2 = (String)servletContext.getAttribute("key2");
        pw.println(value);
        pw.println(value2);

        pw.flush();
        pw.close();
    }
}
